{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0791599a",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 0.代码的注释\n",
    "\n",
    "> 1.用户输入\n",
    "\n",
    "> 2.格式化输入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9018d15",
   "metadata": {},
   "source": [
    "## 0.代码的注释\n",
    "> 1.#,一般是写在代码块的最上方，或者在每一行代码的最后，用于记录代码思路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "915ee8d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这里写啥都不会显现出来哦，因为有#号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dc682b9",
   "metadata": {},
   "source": [
    "## 1.用户输入\n",
    "\n",
    "> 1.input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "641a8f15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:我是账号\n",
      "密码:我是密码\n",
      "我是账号\n",
      "我是密码\n"
     ]
    }
   ],
   "source": [
    "# 登录系统\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "474810f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：123\n",
      "密码：321\n",
      "欢迎123登录\n"
     ]
    }
   ],
   "source": [
    "# 完善登录系统\n",
    "# 假设我们数据库中有用户信息：账号；123，密码：321\n",
    "user1 = \"123\"\n",
    "password1 = \"321\"\n",
    "\n",
    "username=input(\"账号：\")\n",
    "password=input(\"密码：\")\n",
    "\n",
    "# 场景一：输入正确，登录成功\n",
    "if user1==username and password1==password:\n",
    "    print(\"欢迎\"+user1+\"登录\")\n",
    "# 场景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "beec82ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：111\n",
      "密码：111\n",
      "账号不存在或密码不正确！\n",
      "您还有2次机会输入\n",
      "账号：111\n",
      "密码：111\n",
      "账号不存在或密码不正确！\n",
      "您还有1次机会输入\n",
      "账号：111\n",
      "密码：111\n",
      "三次输入失败，请1分钟后尝试\n"
     ]
    }
   ],
   "source": [
    "# 升级版：增加循环功能，多次错误禁封\n",
    "# 假设我们数据库中有用户信息：账号；123，密码：321\n",
    "user1 = \"123\"\n",
    "password1 = \"321\"\n",
    "\n",
    "for i in range(3,0,-1):  # i 属于 int，无法与str相加\n",
    "    username=input(\"账号：\")\n",
    "    password=input(\"密码：\")\n",
    "\n",
    "# 场景一：输入正确，登录成功\n",
    "    if user1==username and password1==password:\n",
    "        print(\"欢迎\"+user1+\"登录\")\n",
    "        break # 打断，终止循环\n",
    "# 场景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1分钟后尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确！\")\n",
    "            print(\"您还有\"+str(i-1)+\"次机会输入\") # str强制将int转换成str，使之可以相加\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4b86c8c",
   "metadata": {},
   "source": [
    "## 2.格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4bcd1cfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户注册系统 \n",
    "#方法一：字符串相加（拼接）\n",
    "#（略）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4bbf578",
   "metadata": {},
   "outputs": [],
   "source": [
    "# input 作为用户输入，都以字符串（str）形式转回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1b59c6e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：张智豪\n",
      "年龄：20\n",
      "工作：乞讨\n",
      "年收入：20元\n",
      "地区：四海为家\n",
      "---INFO OF 张智豪---\n",
      "年龄：20\n",
      "工作：乞讨\n",
      "年收入：20元\n",
      "地区：四海为家\n"
     ]
    }
   ],
   "source": [
    "# 方法二：占位符 %s,%(这里依次输入要显示的信息)\n",
    "name=input(\"昵称：\")\n",
    "age=input(\"年龄：\")\n",
    "job=input(\"工作：\")\n",
    "salary=input(\"年收入：\")\n",
    "reg=input(\"地区：\")\n",
    "\n",
    "info = '''---INFO OF %s---\n",
    "年龄：%s\n",
    "工作：%s\n",
    "年收入：%s\n",
    "地区：%s'''%(name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fa07dc87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称：张智豪\n",
      "年龄：20\n",
      "工作：摸鱼\n",
      "年收入：200块\n",
      "地区：海边喜欢浪\n",
      "---INFO OF张智豪---\n",
      "姓名：张智豪\n",
      "年龄：20\n",
      "工作：摸鱼\n",
      "年收入：200块\n",
      "地区：海边喜欢浪\n"
     ]
    }
   ],
   "source": [
    "# 方法三：format() 格式化输入\n",
    "name=input(\"昵称：\")\n",
    "age=input(\"年龄：\")\n",
    "job=input(\"工作：\")\n",
    "salary=input(\"年收入：\")\n",
    "reg=input(\"地区：\")\n",
    "\n",
    "info='''---INFO OF{姓名}---\n",
    "姓名：{姓名}\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "年收入：{年收入}\n",
    "地区：{地区}'''.format(地区=reg,工作=job,姓名=name,年龄=age,年收入=salary) # 使用format() 可以直接固定输入，不用规定顺序\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b2bd819",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
